angular.module('mvcApp.push-admin', [
    'ngRoute',
    'ngResource'
]).config(['$routeProvider', function ($routeProvider) {
    $routeProvider
        .when('/push-admin', {
            templateUrl: 'static/views/push-admin.html',
            controller: 'PushAdminCtrl'
        });
}]).controller('PushAdminCtrl', ['$scope', '$http', 'Category', 'Shop', 'Product', 'User',
    function ($scope, $http, Category, Shop, Product, User) {
        $scope.wechatPushes = [];
        $scope.pushsPage = 1;
        $scope.pageSize = 10;
        $scope.wechatHistories = [];
        $scope.loadWechatPushes = function () {
            if ($scope.pushsPage < 1) {
                $scope.pushsPage = 1;
            }
            User.listWechatPushContents($scope.pushsPage, $scope.pageSize)
                .then(function (pushes) {
                    $scope.wechatPushes = pushes;
                    $scope.$applyAsync();
                }).catch(function (err) {
                    if (err) {
                        console.log(err);
                    }
                });
        };
        $scope.viewWechatPushContent = function (wechatPush) {
            $scope.readOnly = true;
            $scope.pushItems = JSON.parse(wechatPush.content);
            $scope.currentPushContentId = wechatPush.id;
            $scope.selectPushItem($scope.pushItems[0]);
            $scope.$applyAsync();
            User.listWechatPushHistoriesOfContent(wechatPush.id)
                .then(function (data) {
                    $scope.wechatHistories = data;
                    _.each($scope.wechatHistories, function (item) {
                        User.getUserInfoWithCache(item.userId)
                            .then(function (userInfo) {
                                item.user = userInfo;
                                $scope.$applyAsync();
                            }, function (err) {
                                console.log(err);
                            });
                    });
                    $scope.$applyAsync();
                }, function (err) {
                    alert(err);
                });
        };
        $scope.loadWechatPushes();
        $scope.pushItems = [{}];
        $scope.readOnly = false;
        $scope.selectPushItem = function (pushItem) {
            $scope.currentPushItem = pushItem;
        };
        $scope.selectPushItem($scope.pushItems[0]);
        $scope.onUploadedLastMediaFile = function (fileId) {
            //if ($scope.pushItems.length > 0) {
            //    $scope.pushItems[$scope.pushItems.length - 1].mediaFileId = fileId;
            //    $scope.$applyAsync();
            //}
            $scope.currentPushItem.mediaFileId = fileId;
            $scope.$applyAsync();
        };
        $scope.removeWechatPushContent = function (wechatPushContent) {
            if (!window.confirm('确认删除此微信图文消息?')) {
                return;
            }
            User.deleteWechatPushContent(wechatPushContent.id)
                .then(function () {
                    alert('删除成功');
                }, function (err) {
                    alert(err);
                });
        };
        $scope.saveWechatPushContent = function (pushItems) {
            var pushIt = window.confirm('是否同时推送(有每天次数限制)?');
            pushIt = pushIt || false;
            pushItems = _.filter(pushItems, function (pushItem) {
                return pushItem.title && pushItem.content && pushItem.mediaFileId;
            });
            if (pushItems.length < 1) {
                alert("图文消息不可为空");
                return;
            }
            $http({
                url: '/create_wechat_push',
                method: 'POST',
                headers: formHeaders,
                data: $.param({
                    contentId: $scope.currentPushContentId,
                    type: 0,
                    push: pushIt,
                    title: pushItems[0].title,
                    content: JSON.stringify(pushItems)
                })
            }).success(function (data) {
                if (data.code === 0) {
                    alert("保存成功");
                    $scope.loadWechatPushes();
                } else {
                    alert(data.data);
                }
            }).catch(function (err) {
                alert(err);
            });
        };

        $scope.pushWechatContent = _.throttle(function (contentId) {
            if (!contentId) {
                return;
            }
            if (!window.confirm('确认推送?')) {
                return;
            }
            User.pushWechatPushContent(contentId)
                .then(function (data) {
                    alert("推送成功");
                }, function (err) {
                    alert(err);
                });
        }, 2500);

        $scope.pushToWechat = function (pushItems) {
            pushItems = _.filter(pushItems, function (pushItem) {
                return pushItem.title && pushItem.content && pushItem.mediaFileId;
            });
            if (pushItems.length < 1) {
                alert("图文消息不可为空");
                return;
            }
            $http({
                url: '/wechat_push',
                method: 'POST',
                headers: formHeaders,
                data: $.param({
                    type: 0,
                    title: pushItems[0].title,
                    content: JSON.stringify(pushItems)
                })
            }).success(function (data) {
                if (data.code === 0) {
                    alert("发送成功");
                    $scope.loadWechatPushes();
                } else {
                    alert(data.data);
                }
            }).catch(function (err) {
                alert(err);
            });
        };
        $scope.createWechatPushNews = function () {
            $scope.readOnly = false;
            $scope.currentPushContentId = null;
            $scope.pushItems = [{}];
            $scope.selectPushItem($scope.pushItems[0]);
        };
        $scope.addNewItemToPushNews = function () {
            if ($scope.pushItems.length < 1) {
                $scope.pushItems.push({});
                return;
            }
            var last = $scope.pushItems[$scope.pushItems.length - 1];
            if (last.title && last.content && last.mediaFileId) {
                $scope.pushItems.push({});
                $scope.selectPushItem($scope.pushItems[$scope.pushItems.length - 1]);
            }
        };
        $scope.removePushItem = function (pushItem) {
            $scope.pushItems.remove(pushItem);
            $scope.$applyAsync();
        };
    }]);